import {defineStore} from 'pinia'
import {store} from '../index'

export interface UserType {
  username: string
  password: string
}

interface UserState {
  userInfo?: UserType
  tokenKey: string
  token: string
}

export const useUserStore = defineStore('user', {
  state: (): UserState => {
    return {
      userInfo: undefined,
      tokenKey: 'Authorization',
      token: '',
    }
  },
  getters: {
    getTokenKey(): string {
      return this.tokenKey
    },
    getToken(): string {
      return this.token
    },
    getUserInfo(): UserType | undefined {
      return this.userInfo
    },
  },
  actions: {
    setTokenKey(tokenKey: string) {
      this.tokenKey = tokenKey
    },
    setToken(token: string) {
      this.token = token
    },
    setUserInfo(userInfo?: UserType) {
      this.userInfo = userInfo
    },
  },
  persist: true
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
